
import path from 'path';
import  fs from 'fs';
import  OSS from 'ali-oss'
import { fileURLToPath } from 'url';
let lastfiles= {}


const __filename = fileURLToPath(import.meta.url);

// 获取当前模块所在的目录路径
const __dirname = path.dirname(__filename);


let newLast = {}; //本次最新生成所有文件
let specfile={"index.html":1,"test.js":1}
let ossconfig = {
	path: "dist", //发布后的目录所在文件夹名字
	osspath: "" //oss文件所在目录
}
//805059123@QQ.COM
let client = new OSS({
	region: 'oss-cn-heyuan',
	accessKeyId: 'LTAI5tPUhWL1Ws2Ga2x9ufkc',
	accessKeySecret: 'ZLs0yGY0FNc2UvKjrv0qjBZTKFXWrr',
	bucket: 'zhongyan-guanwang'
});






let targetPath = path.join(__dirname,ossconfig.path)

function readFileList(path, filesList) {
	var files = fs.readdirSync(path);
	files.forEach(function(itm, index) {

		var stat = fs.statSync(path + "/" + itm);
		if (stat.isDirectory()) {
			//递归读取文件
			readFileList(path + "/" + itm, filesList)
		} else {

			var obj = {}; //定义一个对象存放文件的路径和名字
			obj.path = path + "/"; //路径
			obj.filename = itm //名字

			if (path.indexOf("ueditor") >= 0) {
				console.log("忽略ueditor")
			} else {
				if (!lastfiles[obj.filename]) {
					lastfiles[obj.filename] = 1
					newLast[obj.filename] = 1
					filesList.push(obj);
				} else {
					if(!specfile[obj.filename]){
						newLast[obj.filename] = 1
					}else{
						filesList.push(obj);
					}
					
				}

			}

		}

	})

}




async function put(file) {
	try {
		console.log(ossconfig.osspath + file.path.replace(targetPath, "") + file.filename, file
			.path +
			file.filename);
		let result = await client.put(ossconfig.osspath + file.path.replace(targetPath, "") + file.filename, file
			.path +
			file.filename);
		console.log("上传成功:" + file.filename)
		//console.log(result);
	} catch (e) {
		console.log("上传失败：" + file.filename + "重试第一次");
		try {
			let result = await client.put(ossconfig.osspath + file.path.replace(targetPath, "") + file.filename,
				file.path +
				file.filename);
			//console.log(result);
		} catch (e) {
			console.log("上传失败：" + file.filename + "重试第二次");
			try {
				let result = await client.put(ossconfig.osspath + file.path.replace(targetPath, "") + file.filename,
					file.path +
					file.filename);
				//console.log(result);
			} catch (e) {
				console.log("上传失败：" + file.filename + "请重试");

			}
		}
	}
	return true
}



async function getNextFile(i, filesList) {
	let result = await put(filesList[i]);
	let total = filesList.length;
	console.log("上传进度：" + parseInt(((i + 1) / total) * 1000) / 10);
	if (i < total - 1) {
		getNextFile(i + 1, filesList)
	} else if (i == total - 1) {
		writeHistory()
	}

}


function getFileList(path) {
	var filesList = [];
	readFileList(path, filesList);
	let uploadcount = 0;
	if(filesList.length>0){
		getNextFile(uploadcount, filesList);
	}else{
		console.log("无最新文件，不用上传！")
	}
	
}

function writeHistory() {

	let targetPath = __dirname + "\\last.js"
	// 异步写入数据到文件
	fs.writeFile(targetPath, "export default " + JSON.stringify(newLast, null, 4), {
		encoding: 'utf8'
	}, ok => {
		console.log("写入成功")
	})

}

getFileList(targetPath)
